<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        .box{
            position:relative;
            width: 600px;
            height: 400px;
            margin: 50px auto;
            border:20px solid white;
            box-sizing: border-box;
            box-shadow: 0 0 5px black;
            overflow:hidden;
        }
        .img{
            width:100%;
            height: 100%;
        }
        .bottom{
            position: absolute;
            bottom:0;
            width: 100%;
            padding: 30px;
            box-sizing: border-box;
            color:white;
        }
        .title{
            margin: 0;
        }
        .dots{
            position: absolute;
            bottom: 10px;
            left:110px;
            transform: translate(-50%);
            display: flex;
            gap:10px;
        }
        .dot{
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color:grey;
            cursor:pointer;
        }
        .dot.active{
            background-color: #fff;
        }
    </style>
</head>
<body>
    <div class="box">
        < img src="" class="img" alt="图片">
        <div class="bottom">
            <h4 class="title"></h4>
        </div>
        <div class="dots">
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
        </div>
    </div>
    <script>
        const sliderData = [
      { url: '../../作业/images/course01.png', title: '对人类来说会不会太超前了？', color: 'rgb(100, 67, 68)' },
      { url: '../../作业/images/course02.png', title: '开启剑与雪的黑暗传说！', color: 'rgb(43, 35, 26)' },
      { url: '../../作业/images/course03.png', title: '真正的jo厨出现了！', color: 'rgb(36, 31, 33)' },
      { url: '../../作业/images/course04.png', title: '李玉刚：让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
      { url: '../../作业/images/course05.png',title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
      { url: '../../作业/images/course06.png', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
      { url: '../../作业/images/course07.png', title: '一站式解决你的电脑配置问题！！！', color: 'rgb(53, 29, 25)' },
      { url: '../../作业/images/course08.png', title: '谁不想和小猫咪贴贴呢！', color: 'rgb(99, 72, 114)' },
    ] 
   const randomIndex = Math.floor(Math.random() * sliderData.length);
    const randomItem = sliderData[randomIndex];

    const img = document.querySelector('.img');
    const title = document.querySelector('.title');
    const bottom = document.querySelector('.bottom');
    const dots = document.querySelectorAll('.dot');

    img.src = randomItem.url;
    title.textContent = randomItem.title;
    bottom.style.backgroundColor = randomItem.color;
    dots[randomIndex].classList.add('active');

    let index =0
    function updateSlider() {
             const data = sliderData[index];
             // 更新图片、标题、底部背景色
             img.src = data.url;
            title.textContent = data.title;
             bottom.style.backgroundColor = data.color;
             // 用for循环替代forEach，更新圆点状态
             for (let i = 0; i < dots.length; i++) {
                 if (i === index) {
                    dots[i].classList.add('active');
                 } else {
                    dots[i].classList.remove('active');
                 }
             }
             // 实现轮播循环
             index = (index + 1) % sliderData.length;
         }
         // 初始化显示第一张
         updateSlider();
         // 每隔2秒自动切换
         setInterval(updateSlider, 2000);

    </script>
</body>
</html>